package com.ruoyi.utils;

import com.ruoyi.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.List;

/**
 * 类作用：根据坐标点画检测框
 *
 * @author Mr-Le
 */
@Slf4j
public class ImgDrawCircleTool {
    /**
     * 根据坐标点、标签集合画 检测框
     *
     * @param list      坐标点集合
     * @param tag       标签集合
     * @param inImgPath 输入图片路径
     * @return 返回结果图路径
     * @Param num1 种类1的数量
     * @Param num2 种类2的数量
     */
    public String doDrawRectangle(List<int[]> list, List<String> tag, String inImgPath, Integer num1, Integer num2, Integer num3, Integer num4,Integer num5, Integer num6, Integer num7, Integer num8,Integer num9, Integer num10, Integer num11, Integer num12,Integer num13, Integer num14, Integer num15) {
        String resPath = null;
        String s = StringUtils.substringAfter(inImgPath, "profile");

        File file = new File(inImgPath);

        //if (list.isEmpty()) {
        //    return null;
        //}
        if (file.exists()) {
            log.info("file exists!");
            // 原图加上 result 标签
            resPath = addResult(inImgPath);
            try {
                BufferedImage image = ImageIO.read(new File(inImgPath));
                Graphics graphics = image.getGraphics();
                Graphics2D g = (Graphics2D) graphics;
                g.setStroke(new BasicStroke(5.0f));

                if (list.size() == tag.size()) {
                    for (int i = 0; i < list.size(); i++) {
                        Color newColor = judgeColorByTag(tag.get(i));
                        g.setColor(newColor);
                        // 检测框 坐标点 转换 x1,y1;x2,y2----------> x,y,w,h
                        g.drawRect(list.get(i)[0], list.get(i)[1], list.get(i)[2] - list.get(i)[0], list.get(i)[3] - list.get(i)[1]);
                    }
                    // new: num1:高龄若虫 num2:低龄若虫 num3:长翅飞虱 num4:短翅飞虱
                            /*
                            长翅飞虱	M-adult
                            短翅飞虱	B-adult
                            低龄若虫	J-nymph
                            高龄若虫	S-nymph
                            */
                    // 增加图例，每种虫有多少头显示 替换
//                    String numData1 = "S-nymph:" + num1;
//                    String numData2 = "J-nymph:" + num2;
//                    String numData3 = "M-adult:" + num3;
//                    String numData4 = "B-adult:" + num4;
//                    String numData1 = "M-BPH-adult:"+retKong(5)+num1.toString();//2
//                    String numData2 = "B-BPH-adult:"+retKong(6)+num2.toString();
//                    String numData3 = "S-BPH-nymph:"+retKong(3)+num3.toString();
//                    String numData4 = "M-WBPH-adult:"+retKong(2)+num4.toString();//1
//                    String numData5 = "B-WBPH-adult:"+retKong(3)+num5.toString();
//                    String numData6 = "S-WBPH-nymph:"+retKong(0)+num6.toString();
//                    String numData7 = "J-RPH-nymph:"+retKong(4)+num7.toString();//2
//                    String numData8 = "M-SBPH-adult:"+retKong(4)+num8.toString();//1
//                    String numData9 = "B-SBPH-adult:"+retKong(5)+num9.toString();
//                    String numData10 ="S-SBPH-nymph:"+retKong(2)+num10.toString();
//                    String numData11 ="Spider:"+retKong(17)+num11.toString();//7
//                    String numData12 ="Miridae:"+retKong(16)+num12.toString();//6
//                    String numData13 ="Rove-beetle:"+retKong(9)+num13.toString();//2
//                    String numData14 ="RLH-adult:"+retKong(12)+num14.toString();//4
//                    String numData15 ="RLH-nymph:"+retKong(9)+num15.toString();
                    String numData1 = "M-BPH-adult:"+retKong(1)+num1.toString();//2
                    String numData2 = "B-BPH-adult:"+retKong(1)+num2.toString();
                    String numData3 = "S-BPH-nymph:"+retKong(1)+num3.toString();
                    String numData4 = "M-WBPH-adult:"+retKong(0)+num4.toString();//1
                    String numData5 = "B-WBPH-adult:"+retKong(0)+num5.toString();
                    String numData6 = "S-WBPH-nymph:"+retKong(0)+num6.toString();
                    String numData7 = "J-RPH-nymph:"+retKong(1)+num7.toString();//2
                    String numData8 = "M-SBPH-adult:"+retKong(0)+num8.toString();//1
                    String numData9 = "B-SBPH-adult:"+retKong(0)+num9.toString();
                    String numData10 ="S-SBPH-nymph:"+retKong(0)+num10.toString();
                    String numData11 ="Spider:"+retKong(6)+num11.toString();//7
                    String numData12 ="Miridae:"+retKong(5)+num12.toString();//6
                    String numData13 ="Rove-beetle:"+retKong(1)+num13.toString();//2
                    String numData14 ="RLH-adult:"+retKong(3)+num14.toString();//4
                    String numData15 ="RLH-nymph:"+retKong(3)+num15.toString();
                    // 增加图例，每种虫有多少头显示
                    int font_size = calcu_font_size(image.getWidth(),image.getHeight())-40;
//                    g.setFont(new Font("Time New Roman", Font.BOLD, 64));
                    g.setFont(new Font("Time New Roman", Font.BOLD, font_size));
                    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
                    g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
                    // -400
                    // y: 80 160 240 320
                    int baseY  = font_size+(int)Math.ceil(0.125*font_size);
                    int letfX = font_size*9;//(int)(Math.ceil(image.getWidth()/9.6)*1.2);
//                    g.setColor(Color.RED);
//                    g.drawString(numData3, image.getWidth() - letfX, baseY);
//                    g.setColor(Color.BLUE);
//                    g.drawString(numData4, image.getWidth() - letfX, baseY*2);
//                    g.setColor(Color.YELLOW);
//                    g.drawString(numData1, image.getWidth() - letfX, baseY*3);
//                    g.setColor(Color.magenta);
//                    g.drawString(numData2, image.getWidth() - letfX, baseY*4);
                    g.setColor(Color.RED);
                    g.drawString(numData1, image.getWidth() - letfX, baseY);
                    g.setColor(Color.BLUE);
                    g.drawString(numData2, image.getWidth() - letfX, baseY*2);
                    g.setColor(Color.YELLOW);
                    g.drawString(numData3, image.getWidth() - letfX, baseY*3);
                    g.setColor(Color.magenta);
                    g.drawString(numData4, image.getWidth() - letfX, baseY*4);
                    g.setColor(Color.cyan);
                    g.drawString(numData5, image.getWidth() - letfX, baseY*5);
                    g.setColor(Color.darkGray);
                    g.drawString(numData6, image.getWidth() - letfX, baseY*6);
                    g.setColor(Color.green);
                    g.drawString(numData7, image.getWidth() - letfX, baseY*7);
                    g.setColor(Color.orange);
                    g.drawString(numData8, image.getWidth() - letfX, baseY*8);
                    g.setColor(new Color(0,199,140));
                    g.drawString(numData9, image.getWidth() - letfX, baseY*9);
                    g.setColor(new Color(255,125,64));
                    g.drawString(numData10, image.getWidth() - letfX, baseY*10);
                    g.setColor(new Color(94,38,18));
                    g.drawString(numData11, image.getWidth() - letfX, baseY*11);
                    g.setColor(new Color(255,97,0));
                    g.drawString(numData12, image.getWidth() - letfX, baseY*12);
                    g.setColor(new Color(8,46,84));
                    g.drawString(numData13, image.getWidth() - letfX, baseY*13);
                    g.setColor(new Color(0,201,87));
                    g.drawString(numData14, image.getWidth() - letfX, baseY*14);
                    g.setColor(new Color(135,38,87));
                    g.drawString(numData15, image.getWidth() - letfX, baseY*15);

                    FileOutputStream fileOutputStream = new FileOutputStream(resPath);
                    ImageIO.write(image, "jpeg", fileOutputStream);
                    fileOutputStream.close();

                } else {
                    log.info("tag和corner对应出错，检查算法端!");
                    return null;
                }
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        } else {
            log.info("文件不存在!");
            return null;
        }
        return resPath;
    }

    /**
     * 根据坐标点、标签集合画 检测框
     *
     * @param inImgPath 输入图片路径
     * @return 返回结果图路径
     *
     */
    public String doDrawEmpty(String inImgPath) throws FileNotFoundException {
        String resPath = null;
        String s = StringUtils.substringAfter(inImgPath, "profile");

        File file = new File(inImgPath);

        if (file.exists()) {
            log.info("file exists!");
            // 原图加上 result 标签
            resPath = addResult(inImgPath);
            try {
                BufferedImage image = ImageIO.read(new File(inImgPath));
                Graphics graphics = image.getGraphics();
                Graphics2D g = (Graphics2D) graphics;
                g.setStroke(new BasicStroke(5.0f));

                // new: num1:高龄若虫 num2:低龄若虫 num3:长翅飞虱 num4:短翅飞虱
                            /*
                            长翅飞虱	M-adult
                            短翅飞虱	B-adult
                            低龄若虫	J-nymph
                            高龄若虫	S-nymph
                            */
                // 增加图例，每种虫有多少头显示 替换
                String numData1 =  "M-BPH-adult:  0";
                String numData2 =  "B-BPH-adult:  0";
                String numData3 =  "S-BPH-nymph:  0";
                String numData4 =  "M-WBPH-adult: 0";
                String numData5 =  "B-WBPH-adult: 0";
                String numData6 =  "S-WBPH-nymph: 0";
                String numData7 =  "J-RPH-nymph:  0";
                String numData8 =  "M-SBPH-adult: 0";
                String numData9 =  "B-SBPH-adult: 0";
                String numData10 = "S-SBPH-nymph: 0";
                String numData11 = "Spider:       0";
                String numData12 = "Miridae:      0";
                String numData13 = "Rove-beetle:  0";
                String numData14 = "RLH-adult:    0";
                String numData15 = "RLH-nymph:    0";

                // 增加图例，每种虫有多少头显示
                int font_size = calcu_font_size(image.getWidth(),image.getHeight())-40;
//                g.setFont(new Font("Time New Roman", Font.BOLD, 64));
                g.setFont(new Font("Time New Roman", Font.BOLD, font_size));
                g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
                g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));

//                g.setColor(Color.RED);
//                g.drawString(numData1, image.getWidth() - 400, 80);
//                g.setColor(Color.BLUE);
//                g.drawString(numData2, image.getWidth() - 400, 160);
//                g.setColor(Color.green);
//                g.drawString(numData3, image.getWidth() - 400, 240);
//                g.setColor(Color.magenta);
//                g.drawString(numData4, image.getWidth() - 400, 320);
                // -400
                // y: 80 160 240 320
                int baseY  = font_size+(int)Math.ceil(0.125*font_size);
                int letfX = font_size*9;//(int)(Math.ceil(image.getWidth()/9.6)*1.2);
//                g.setColor(Color.RED);
////                g.drawString(numData1, image.getWidth() - letfX, baseY);
////                g.setColor(Color.BLUE);
////                g.drawString(numData2, image.getWidth() - letfX, baseY*2);
////                g.setColor(Color.YELLOW);
////                g.drawString(numData3, image.getWidth() - letfX, baseY*3);
////                g.setColor(Color.magenta);
////                g.drawString(numData4, image.getWidth() - letfX, baseY*4);
////                g.setColor(Color.cyan);
////                g.drawString(numData5, image.getWidth() - letfX, baseY*5);
////                g.setColor(Color.darkGray);
////                g.drawString(numData6, image.getWidth() - letfX, baseY*6);
////                g.setColor(Color.green);
////                g.drawString(numData7, image.getWidth() - letfX, baseY*7);
////                g.setColor(Color.orange);
////                g.drawString(numData8, image.getWidth() - letfX, baseY*8);
////                g.setColor(Color.pink);
////                g.drawString(numData9, image.getWidth() - letfX, baseY*9);
////                g.setColor(new Color(255,125,64));
////                g.drawString(numData10, image.getWidth() - letfX, baseY*10);
////                g.setColor(new Color(112,128,105));
////                g.drawString(numData11, image.getWidth() - letfX, baseY*11);
////                g.setColor(new Color(227,168,105));
////                g.drawString(numData12, image.getWidth() - letfX, baseY*12);
////                g.setColor(new Color(255,235,205));
////                g.drawString(numData13, image.getWidth() - letfX, baseY*13);
////                g.setColor(new Color(0,201,87));
////                g.drawString(numData14, image.getWidth() - letfX, baseY*14);
////                g.setColor(new Color(135,38,87));
////                g.drawString(numData15, image.getWidth() - letfX, baseY*15);
                g.setColor(Color.RED);
                g.drawString(numData1, image.getWidth() - letfX, baseY);
                g.setColor(Color.BLUE);
                g.drawString(numData2, image.getWidth() - letfX, baseY*2);
                g.setColor(Color.YELLOW);
                g.drawString(numData3, image.getWidth() - letfX, baseY*3);
                g.setColor(Color.magenta);
                g.drawString(numData4, image.getWidth() - letfX, baseY*4);
                g.setColor(Color.cyan);
                g.drawString(numData5, image.getWidth() - letfX, baseY*5);
                g.setColor(Color.darkGray);
                g.drawString(numData6, image.getWidth() - letfX, baseY*6);
                g.setColor(Color.green);
                g.drawString(numData7, image.getWidth() - letfX, baseY*7);
                g.setColor(Color.orange);
                g.drawString(numData8, image.getWidth() - letfX, baseY*8);
                g.setColor(new Color(0,199,140));
                g.drawString(numData9, image.getWidth() - letfX, baseY*9);
                g.setColor(new Color(255,125,64));
                g.drawString(numData10, image.getWidth() - letfX, baseY*10);
                g.setColor(new Color(94,38,18));
                g.drawString(numData11, image.getWidth() - letfX, baseY*11);
                g.setColor(new Color(255,97,0));
                g.drawString(numData12, image.getWidth() - letfX, baseY*12);
                g.setColor(new Color(8,46,84));
                g.drawString(numData13, image.getWidth() - letfX, baseY*13);
                g.setColor(new Color(0,201,87));
                g.drawString(numData14, image.getWidth() - letfX, baseY*14);
                g.setColor(new Color(135,38,87));
                g.drawString(numData15, image.getWidth() - letfX, baseY*15);


                FileOutputStream fileOutputStream = new FileOutputStream(resPath);
                ImageIO.write(image, "jpeg", fileOutputStream);
                fileOutputStream.close();


            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        } else {
            log.info("文件不存在!");
            return null;
        }
        return resPath;
    }

    /**
     * 根据tag判断检测框颜色
     *
     * @param tag 标签
     * @return 返回颜色
     */
    public static Color judgeColorByTag(String tag) {
        switch (tag) {
            case "1":
                return Color.red;
            case "2":
                return Color.BLUE;
            case "3":
                return Color.YELLOW;
            case "4":
                return Color.magenta;
            case "5":
                return Color.cyan;
            case "6":
                return Color.darkGray;
            case "7":
                return Color.green;
            case "8":
                return Color.orange;
            case "9":
                return new Color(0,199,140);
            case "10":
                return new Color(255,125,64);
            case "11":
                return new Color(94,38,18);
            case "12":
                return new Color(255,97,0);
            case "13":
                return new Color(8,46,84);
            case "14":
                return new Color(0,201,87);
            case "15":
                return new Color(135,38,87);
            default:
                return Color.white;
        }
    }

//    public static Color judgeColorByTag(String tag) {
//        switch (tag) {
//            case "echinus":
//                return Color.red;
//            case "holothurian":
//                return Color.GREEN;
//            case "starfish":
//                return Color.BLUE;
//            default:
//                return Color.ORANGE;
//        }
//    }

    /**
     * 添加标签：_Result 标签
     *
     * @param srcPath 字符串路径
     * @return 返回加标签之后的字符串
     */
    private static String addResult(String srcPath) {
        String res = "";
        String[] str = new String[2];
        /* \\. 转义字符，相当于 . ,分隔出.前后的内容 */
        str = srcPath.split("\\.");
        res = str[0] + "__Result." + str[1];
        return res;
    }

    private int calcu_font_size(int width, int height){
        double x = 80.0/(3840*2160);
        double image_size = width*height*1.0;
        return (int) Math.ceil(x*image_size);
    }
    private String retKong(int num){
        String ret = "";
        for(int i=0;i<num;i++){
            ret = ret + " ";
        }
        return ret;
    }
    /**
     * 根据坐标点集合画 检测框
     * @param list 坐标点集合
     * @param inImgPath 输入图片路径
     * @return 返回结果图路径
     */
   /* public String doDrawRectangle(List<int[]> list, String inImgPath) {
        String resPath = null;
        String s = StringUtils.substringAfter(inImgPath, "profile");

        File file = new File(inImgPath);

        if (list.isEmpty()) {
            return null;
        }
        if (file.exists()) {
            System.out.println("file exists!");
            // 原图加上 result 标签
            resPath = addResult(inImgPath);
            try {
                BufferedImage image = ImageIO.read(new File(inImgPath));
                Graphics graphics = image.getGraphics();
                Graphics2D g = (Graphics2D) graphics;
                g.setColor(Color.red);
                g.setStroke(new BasicStroke(7.0f));
                for (int[] ints : list) {
                    // 检测框 坐标点 转换 x1,y1;x2,y2----------> x,y,w,h
                    g.drawRect(ints[0], ints[1], ints[2] - ints[0], ints[3] - ints[1]);
                }
                FileOutputStream fileOutputStream = new FileOutputStream(resPath);
                ImageIO.write(image, "jpeg", fileOutputStream);
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        } else {
            System.out.println("文件不存在");
            return null;
        }
        return resPath;
    }*/
}
